<?php

class Sources_lib
{
	var $CI;
	function __construct()
	{
		$this->CI = & get_instance();
		$this->CI->load->library("category_lib");
		
	}
	
	function inc_click($id)
	{
		$this->CI->load->model('sources_model');
		$sql = "update sources set clicknum=clicknum+1 where id='$id'";
		$ret = $this->CI->sources_model->execute($sql);
		return $ret;
	
	}
	
	function object2array($obj,$islist=false)
	{
		if(is_array($obj))
		{
			$data = $obj;
		}else
		{
			$data[] = $obj;
			//$islist = true;
		}
		$nlist = array();	
				
		foreach($data as $row)
		{
				$temp = array();
				$temp["realid"] = $row->realid;
				$temp['id'] = $row->id;
				$temp['categoryid'] = $row->categoryid;
				$temp['categoryname'] = $this->CI->category_lib->get_category_name($row->categoryid);
				$temp['pubdate'] =  $row->pubdate;
				if($islist)
				{
					$temp['description'] =  mb_substr($row->description,0,500);
				}
				else
				{
					$temp['description'] =  $row->description;
					$temp['description_en'] =  $row->description_en;
				}
				$temp['title'] =  $row->title;
				$temp['username'] =  $row->username;
				$temp['link'] =  "/source/detail/{$row->id}/{$row->username}";
				$temp['downlink'] = $this->getDownloadAddr($row);
				$nlist[] = $temp;
				
		}
		//print_r($data);var_dump($islist);
		if(count($data)==1 and $islist==false)
			return $nlist[0];
		else
			return $nlist;					
	}
	
	function get_list($catid,$pageno=1,$pagesize=8)
	{
		$this->CI->load->model('sources_model');
		$condition="";
		if($catid>0)
		{
			if($catid % 1000 ==0)
				$condition =  array("categoryid >"=>$catid,"categoryid <"=>$catid+1000);
			else
				$condition =  array("categoryid"=>$catid);
		}
		
		//19002
		
		$ret = $this->CI->sources_model->getListEx($condition,"id desc",$pageno,$pagesize);
		$a = $this->object2array($ret['contentList'],true);
		$ret['contentList'] = $a;
		
		return $ret;
		
	}
	
	function get_list_by_username($username,$pageno=1,$pagesize=8)
	{
		$this->CI->load->model('sources_model');
		$condition =  array("username"=>$username);
		
		$ret = $this->CI->sources_model->getListEx($condition,"id desc",$pageno,$pagesize);
		$a = $this->object2array($ret['contentList'],true);
		$ret['contentList'] = $a;
		return $ret;
		
	}	
	
	function get_prev_source($id)
	{
		$this->CI->load->model('sources_model');
		$condition =  array("id <"=>$id);
		$ret = $this->CI->sources_model->getList($condition,"id desc",1,1);
		$row = array();
		
		if(!empty($ret))
		{
			$row = $this->object2array($ret,false);
		}
		return $row;
		
	}
		
	function get_next_source($id)
	{
		$this->CI->load->model('sources_model');
		$condition =  array("id >"=>$id);
		$ret = $this->CI->sources_model->getList($condition,"id asc",1,1);
		$row = array();
		
		if(!empty($ret))
		{
			$row = $this->object2array($ret,false);
		}
		return $row;
		
	}
			
	function get_info($id)
	{
		$this->CI->load->model('sources_model');
		$ret = $this->CI->sources_model->getInfo($id);
		$nret = $this->object2array($ret);
		return $nret;
	}
		
	/*
	 * clickpnum  下载人数
	 * title
	 * tag
	 * source_money
	 * username
	 * groupname
	 * sourcesize
	 * pubdate
	 * updatetime
	 * description
	 * isrecommended
	 * status  0 用户发布未上传至服务器 1 正常  -1 删除  -3 有病毒 -2 待审核    
	 * 
	 * filetype
	 * originfile
	 * fileaddr
	 * fileaddr_prefix
	 * md5
	 * isrepeat
	 * 用户上传后 status=0,移动文件成功置为1
	 * 
	 * return -1 已上传过相同的资源
	 * return -10 输入错误
	 * 
	 */
	 
	function add($source)
	{
		if($source->title=="" or $source->tag=="" or $source->description=="" or $source->username=='')
			return -10;
		$this->CI->load->model('sources_model');	
		if($this->CI->sources_model->getCount(array('realid'=>$source->realid))>0)
		{
			return -1;
		}
		
		$sourceid = $this->CI->sources_model->insert($source);
		if(intval($sourceid)==0) return -11;
		$source->id = $sourceid;
		return $source;

	}
	
		
	
	
	function getSourceCount($reload=0)
	{
		return $this->getCount(array(),$reload,86400);
		//return $this->getCount(array('status'=>1,'isrepeat'=>0),$reload,86400);
		/*
		$this->CI->load->library('cache');
		$cachekey = "source_count";
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);;
		if(!$ret)
		{		
			$this->CI->load->model('sources_model');
			$ret = $this->CI->sources_model->getCount(array('status'=>1));
			$this->CI->cache->set($cachekey,$ret);
		}
		return $ret;
		*/
		
	}
	

	
	function getDownloadList($sourceid,$limit=10,$reload=0)
	{
		try{
			$this->CI->load->model('userdownload_model');
			$ret = $this->CI->userdownload_model->getList(array('sourceid'=>$sourceid),'downdate desc',1,$limit);		
			return $ret;
			
			$this->CI->load->library('cache');
			$cachekey = "s_dl_{$sourceid}_{$limit}";
			if($reload) 
				$ret= '';
			else
				$ret =  $this->CI->cache->get($cachekey);;
			if(!$ret)
			{
				$this->CI->load->model('userdownload_model');
				$ret = $this->CI->userdownload_model->getList(array('sourceid'=>$sourceid),'downdate desc',1,$limit);
				$this->CI->cache->set($cachekey,$ret);
			}
		}catch (Exception $e) {
			$ret = array();
		}
		return $ret;		
	}
	

	function getInfo_2($sourceid,$reload =0)
	{
		if($sourceid==0)
		{ 
			return false;
		} 
		$this->CI->load->library('cache');
		$cachekey = 'sci_'.$sourceid;
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);;

		if(!$ret)		
		{
			$this->CI->load->model('sources2_model');
			$ret = $this->CI->sources2_model->getInfo(array('id'=>$sourceid));
			$ret->title = htmlspecialchars(trim($ret->title));
			$ret->description = htmlspecialchars(trim($ret->description));
			if($ret->id>0)
			{
				$ret->tag = explode(' ',htmlspecialchars($ret->tag));
				$this->CI->cache->set($cachekey,$ret);
			}
		}
		return $ret;
	}
		
	function getInfo($sourceid,$reload =0)
	{
		if($sourceid==0)
		{ 
			return false;
		} 
		$this->CI->load->library('cache');
		$cachekey = 'sci_'.$sourceid;
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);;

		if(!$ret)		
		{
			$this->CI->load->model('sources_model');
			$ret = $this->CI->sources_model->getInfo(array('id'=>$sourceid));
			$ret->title = htmlspecialchars(trim($ret->title));
			$ret->description = htmlspecialchars(trim($ret->description));
			if($ret->id>0)
			{
				$ret->tag = explode(' ',htmlspecialchars($ret->tag));
				$this->CI->cache->set($cachekey,$ret);
			}
		}
		return $ret;
	}
	
	function setInfo($source)
	{
		$this->CI->load->model('sources_model');
		$this->CI->load->model('sources_tag_model');
		$ret = $this->CI->sources_model->update($source);
		if(property_exists($source,'tag') and property_exists($source,'status') and $source->tag!="" and $source->status==1)
		{			
			$this->CI->sources_tag_model->addTags(explode(' ',$source->tag));			
		}
		if(property_exists($source,'status') and $source->status==-1)
		{
			try{
				$this->CI->sources_tag_model->deleteTags($source->id);
			}catch(Exception $ex){}
		}
		return $ret;
	}
	
	
		

	
	function getDownloadAddr($source,$ds="wt")
	{
		
		if($source->status!=1)
		{
			$dl = 'http://disk.csdn.net/~haoyu/fd_91.php?u='.$source->fileaddr;
			return "";
		}
		$filehost='';
		$script = 'fd.php';
		$sid = $source->realid;
		/*
		if($source->fileaddr_prefix=='/down9')
			$filehost = 'dl7.csdn.net';
		*/		
		//$source['fileaddr']
		//$source['fileaddr_prefix']			
		if($source->fileaddr_prefix=='/down8' or $source->fileaddr_prefix=='/down9' or $source->fileaddr_prefix =='/down10')
		{
			$filehost = 'dl6.csdn.net';
			$script = 'fd.php';
		}								
		if($source->fileaddr_prefix=='/down7')
			$filehost = 'dl5.csdn.net';				
		if($source->fileaddr_prefix=='/down6')
			$filehost = 'dl4.csdn.net';		
		if($source->fileaddr_prefix=='/down5')
			$filehost = 'dl3.csdn.net';
		if($source->fileaddr_prefix=='/down4')
			$filehost = 'dl2.csdn.net';			
		if($source->fileaddr_prefix=='/down1' or $source->fileaddr_prefix=='/down3' or $source->fileaddr_prefix=='/down2')
			$filehost = 'dl1.csdn.net';	
		
		if($ds=='dx')
		//if($ds=='dx' and $filehost!="dl2.csdn.net" and $filehost!="dl4.csdn.net")
		{
			$filehost = "dldx.csdn.net";
		}
		
		if($ds=='wt')
		{
			$filehost = "dlwt.csdn.net";
		}
		if($ds=='dl5')
		{
			$filehost = "dl5.csdn.net";
		}		
				
		if($filehost=='')
		{
			$dl = $source->otherlink;
		}else
		{
			$sid= rand(1000,9999).$sid.rand(1000,9999);
			$dl ="http://$filehost/$script?i=$sid&s=".md5('haoyu'.$sid.date("Ymd"));			
		}
		return $dl;		
	}
	
	function getCount($condition,$reload=0,$time=1200)
	{
		$this->CI->load->library('cache');
		if(is_array($condition))
		{	
			$s = '';
			foreach($condition as $k=>$v)
			{
				$s  .= $k.$v;
			}
			$cachekey = md5("source_count_".$s);	
		}else
			$cachekey = md5("source_count_".$condition);
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);;
		if(!$ret)
		{		
			$this->CI->load->model('sources_model');
			$ret = $this->CI->sources_model->getCount($condition);
			$this->CI->cache->set($cachekey,$ret,$time);
		}
		return $ret;		
	}
	
	function getCountByTag($tag,$reload=0)
	{
		$this->CI->load->library('cache');
		$cachekey = md5("source_count_tag_".$tag);
			
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);;
		if(!$ret)
		{		
			$this->CI->load->model('sources_tag_model');
			$ret = $this->CI->sources_tag_model->getCount(array('status'=>1,'title'=>$tag));
			$this->CI->cache->set($cachekey,$ret,20*60);
		}
		return $ret;		
	}
		
	function getListByTag($tag,$pageno,$pagesize=6,$reload=0)
	{
		$this->CI->load->library('cache');
		if(trim($tag)=='') return array();
		$cachekey = "tag_{$tag}_{$pageno}_{$pagesize}";
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);
		if(!$ret)
		{
			$this->CI->load->model('sources_tag_model');
			if($pageno==0)
			{
				$ret = $this->CI->sources_tag_model->getList(array('status'=>1,'title'=>$tag),'sourceid desc',1,$pagesize,'sourceid');
				$this->CI->cache->set($cachekey,$ret,1200);
			}
			else
			{
				$ret = $this->CI->sources_tag_model->getList(array('status'=>1,'title'=>$tag),'sourceid asc',$pageno,$pagesize,'sourceid');
				$this->CI->cache->set($cachekey,$ret);
			}
			
		}
		arsort($ret);
		$ll = array();
		foreach($ret as $i=>$row)
		{
			$st =  $this->getInfo_2($row->sourceid);
			if($st->status==1)
			{
				$st->description = htmlspecialchars(mb_substr($st->description,0,100,"UTF-8"));
				//$st->description = str_replace("\n","<br/>",$st->description);
				$ll[] = $st ;		
			}
		}		
		return $ll;		
	}
	
	function getRecommendList($pageno,$pagesize=6,$reload=0)
	{
		$this->CI->load->library('cache');
		$cachekey = "recommlist_{$pageno}_{$pagesize}";
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);
		if(!$ret)
		{
			$this->CI->load->model('sources_model');
			if($pageno==0)
			{
				$ret = $this->CI->sources_model->getList(array('status'=>1,'isrecommended'=>11),'id desc',1,$pagesize,'id');
				$this->CI->cache->set($cachekey,$ret,1200);
			}
			else
			{
				$ret = $this->CI->sources_model->getList(array('status'=>1,'isrecommended'=>11),'id asc',$pageno,$pagesize,'id');
				$this->CI->cache->set($cachekey,$ret);
			}
			
		}
		arsort($ret);
		$ll = array();
		foreach($ret as $i=>$row)
		{
			$st =  $this->getInfo_2($row->id);
			if($st->status==1)
			{
				$st->description = htmlspecialchars(mb_substr($st->description,0,100,"UTF-8"));
				//$st->description = str_replace("\n","<br/>",$st->description);
				$ll[] = $st ;		
			}
		}		
		return $ll;
	}
	
	function getLastList($pageno,$pagesize=6,$reload=0)
	{
		$this->CI->load->library('cache');
		$cachekey = "lastlist2_{$pageno}_{$pagesize}";
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);
		if(!$ret)
		{
			$this->CI->load->model('sources_model');
			if ($pageno == 0) $pageno =1;
			//$ret = $this->CI->sources_model->getList(array('status'=>1,'isrepeat'=>0),'id desc',$pageno,$pagesize,'id');
			$ret = $this->CI->sources_model->getList("status=1 AND isrepeat=0 and id>0",'id desc',$pageno,$pagesize,'id');
			$this->CI->cache->set($cachekey,$ret,1200);		
			/*	
			if($pageno==0)
			{
				$ret = $this->CI->sources_model->getList(array('status'=>1,'isrepeat'=>0),'id desc',1,$pagesize,'id');
				$this->CI->cache->set($cachekey,$ret,1200);
			}
			else
			{
				$ret = $this->CI->sources_model->getList(array('status'=>1,'isrepeat'=>0),'id asc',$pageno,$pagesize,'id');
				$this->CI->cache->set($cachekey,$ret);
			}
			*/
			
		}
		arsort($ret);
		$ll = array();
		foreach($ret as $i=>$row)
		{
			$st =  $this->getInfo_2($row->id);
			if($st->status==1)
			{
				$st->description = htmlspecialchars(mb_substr($st->description,0,100,"UTF-8"));
				//$st->description = str_replace("\n","<br/>",$st->description);
				$ll[] = $st ;		
			}
		}		
		return $ll;
	}

	function getListByCategoryid($categoryid,$pageno,$pagesize=6,$reload=0)
	{
		$this->CI->load->library('cache');
		if(trim($categoryid)=='') return array();
		$cachekey = "categoryid_{$categoryid}_{$pageno}_{$pagesize}";
		if($reload) 
			$ret= '';
		else
			$ret =  $this->CI->cache->get($cachekey);
			
		if($categoryid < 100){
			$cid = $categoryid.'000';
			$nextcid = ($categoryid+1).'000';
			$condition = 'status=1 AND categoryid>'.$cid.' AND categoryid<'.$nextcid;
		}else{
			$condition = array('status'=>1,'categoryid'=>$categoryid);
		}
		
		if(!$ret)
		{
			$this->CI->load->model('sources_model');
			if($pageno==0)
			{
				$ret = $this->CI->sources_model->getList($condition,'id desc',1,$pagesize);
				$this->CI->cache->set($cachekey,$ret,1200);
			}
			else
			{
				$ret = $this->CI->sources_model->getList($condition,'id asc',$pageno,$pagesize);
				$this->CI->cache->set($cachekey,$ret);
			}
			
		}
		arsort($ret);
		$ll = array();
		foreach($ret as $i=>$row)
		{
			$st =  $this->getInfo_2($row->id);
			if($st->status==1)
			{
				$st->description = htmlspecialchars(mb_substr($st->description,0,100,"UTF-8"));
				$ll[] = $st ;		
			}
		}		
		return $ll;		
//		return $ret;
	}
}

